################################################################################
##
##  Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
##  Version 2.x, Copyright (C) 2001, Paul Marquess.
##  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
##
##  This program is free software; you can redistribute it and/or
##  modify it under the same terms as Perl itself.
##
################################################################################

=provides

my_snprintf

=implementation

#if !defined(my_snprintf)
#if { NEED my_snprintf }

int
my_snprintf(char *buffer, const Size_t len, const char *format, ...)
{
    dTHX;
    int retval;
    va_list ap;
    va_start(ap, format);
#ifdef HAS_VSNPRINTF
    retval = vsnprintf(buffer, len, format, ap);
#else
    retval = vsprintf(buffer, format, ap);
#endif
    va_end(ap);
    if (retval < 0 || (len > 0 && (Size_t)retval >= len))
        Perl_croak(aTHX_ "panic: my_snprintf buffer overflow");
    return retval;
}

#endif
#endif

=xsinit

#define NEED_my_snprintf

=xsubs

void
my_snprintf()
        PREINIT:
                char buf[128];
                int len;
        PPCODE:
                len = my_snprintf(buf, sizeof buf, "foo%s%d", "bar", 42);
                mXPUSHi(len);
                mXPUSHs(newSVpv(buf, 0));
                XSRETURN(2);

=tests plan => 2

my($l, $s) = Devel::PPPort::my_snprintf();
ok($l, 8);
ok($s, "foobar42");
